/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function inputEvents()
{
    this.listenerMove = Array();
    this.flushInputs = null;
    this.addKeyboardEvent = function(obj)
    {
        this.listenerMove.push(obj);
    };
    //<editor-fold defaultstate="collapsed" desc="moveActions">
    this.actionUP = function()
    {
        for (obj in this.listenerMove)
            this.listenerMove[obj].moveUP();
    };
    this.actionDOWN = function()
    {
        for (obj in this.listenerMove)
            this.listenerMove[obj].moveDOWN();
    };
    this.actionLEFT = function()
    {
        for (obj in this.listenerMove)
            this.listenerMove[obj].moveLEFT();
    };
    this.actionRIGHT = function()
    {
        for (obj in this.listenerMove)
            this.listenerMove[obj].moveRIGHT();
    };
    //</editor-fold>
    this.Init = function()
    {
        //<editor-fold defaultstate="collapsed" desc="variables">
        var interval = 15;
        var left = 37;
        var up = 38;
        var right = 39;
        var down = 40;
        var leftT, upT, rightT, downT;
        var thisEV = this;
        //</editor-fold>
        this.flushInputs = function()
        {
            window.clearInterval(leftT);
            window.clearInterval(upT);
            window.clearInterval(rightT);
            window.clearInterval(downT);
            leftT = upT = rightT = downT = null;
        };
        document.onkeydown = function(event) {
            var key = event.keyCode;
            switch (key)
            {
//                            case 'a'.charCodeAt(0):
//                            case 'A'.charCodeAt(0):
                case left:
                    event.preventDefault();
                    if (leftT == null)
                        leftT = window.setInterval(function() {
                            thisEV.actionLEFT()
                        }, interval);
                    break;
//                            case 's'.charCodeAt(0):
//                            case 'S'.charCodeAt(0):
                case right:
                    event.preventDefault();
                    if (rightT == null)
                        rightT = window.setInterval(function() {
                            thisEV.actionRIGHT()
                        }, interval);
                    break;
//                            case 'w'.charCodeAt(0):
//                            case 'W'.charCodeAt(0):
                case up:
                    event.preventDefault();
                    if (upT == null)
                        upT = window.setInterval(function() {
                            thisEV.actionUP()
                        }, interval);
                    break;
//                            case 'w'.charCodeAt(0):
//                            case 'W'.charCodeAt(0):
                case down:
                    event.preventDefault();
                    if (downT == null)
                        downT = window.setInterval(function() {
                            thisEV.actionDOWN()
                        }, interval);
                    break;
            }
        };
        document.onkeyup = function(event) {
            var key = event.keyCode;
            switch (key)
            {
                case left:
                    window.clearInterval(leftT);
                    leftT = null;
                    break;
                case right:
                    window.clearInterval(rightT);
                    rightT = null;
                    break;
                case up:
                    window.clearInterval(upT);
                    upT = null;
                    break;
                case down:
                    window.clearInterval(downT);
                    downT = null;
                    break;
            }
        };
    };
}

